www.gusucode.com > VC Rijndael算法加密解密示例源码程序-源码程序 > VC Rijndael算法加密解密示例源码程序-源码程序/code/Rijnd/RijndView.cpp
//Download by http://www.NewXing.com // RijndView.cpp : implementation of the CRijndView class // #include "stdafx.h" #include "Rijnd.h" #include "RijndDoc.h" #include "RijndView.h" #include "rijndael.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif //定义 rijndael rij; ///////////////////////////////////////////////////////////////////////////// // CRijndView IMPLEMENT_DYNCREATE(CRijndView, CFormView) BEGIN_MESSAGE_MAP(CRijndView, CFormView) //{{AFX_MSG_MAP(CRijndView) ON_BN_CLICKED(IDC_ENCRYPTION, OnEncryption) ON_BN_CLICKED(IDC_UNCRYPTION, OnUncryption) ON_BN_CLICKED(IDC_ENCRYPTION_SELECT, OnEncryptionSelect) ON_BN_CLICKED(IDC_UNCRYPTION_SELECT, OnUncryptionSelect) //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CRijndView construction/destruction CRijndView::CRijndView() : CFormView(CRijndView::IDD) { //{{AFX_DATA_INIT(CRijndView) m_miyao = _T("shufuxiashufuxiashufuxiashufuxia"); //}}AFX_DATA_INIT // TODO: add construction code here } CRijndView::~CRijndView() { } void CRijndView::DoDataExchange(CDataExchange* pDX) { CFormView::DoDataExchange(pDX); //{{AFX_DATA_MAP(CRijndView) DDX_Text(pDX, IDC_KEY, m_miyao); //}}AFX_DATA_MAP } BOOL CRijndView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CFormView::PreCreateWindow(cs); } void CRijndView::OnInitialUpdate() { CFormView::OnInitialUpdate(); GetParentFrame()->RecalcLayout(); ResizeParentToFit(); } ///////////////////////////////////////////////////////////////////////////// // CRijndView printing BOOL CRijndView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CRijndView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CRijndView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } void CRijndView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/) { // TODO: add customized printing code here } ///////////////////////////////////////////////////////////////////////////// // CRijndView diagnostics #ifdef _DEBUG void CRijndView::AssertValid() const { CFormView::AssertValid(); } void CRijndView::Dump(CDumpContext& dc) const { CFormView::Dump(dc); } CRijndDoc* CRijndView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRijndDoc))); return (CRijndDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CRijndView message handlers void CRijndView::OnEncryption() { // TODO: Add your control notification handler code here k_bit=(unsigned char*)m_miyao.GetBuffer(8); m_miyao.ReleaseBuffer(); unsigned char *e_bit; e_bit=new unsigned char [length]; BeginWaitCursor(); { rij.set_key(k_bit,256); for(int j=0;j<N;j++) { rij.encrypt(var,e_bit); var+=16; e_bit+=16; } var=var-16*N; e_bit=e_bit-16*N; } EndWaitCursor(); char szFilter[] = "all Files(*.*)|*.*||"; CFileDialog FileDlg( FALSE, NULL, NULL, OFN_HIDEREADONLY, szFilter ); FileDlg.m_ofn.lpstrTitle="加密文件存为"; if( FileDlg.DoModal() == IDOK ){ BeginWaitCursor(); CString PathName = FileDlg.GetPathName(); PathName=PathName+name; CFile f; f.Open(PathName, CFile::modeCreate|CFile::modeWrite); f.Write(e_bit,length); f.Close(); EndWaitCursor(); } m_jiami="加密结束!"; MessageBox(m_jiami); delete e_bit; UpdateData(false); Invalidate(true); } void CRijndView::OnUncryption() { // TODO: Add your control notification handler code here k_bit=(unsigned char*)m_miyao.GetBuffer(8); m_miyao.ReleaseBuffer(); //密钥的获取 unsigned char *m_bit; m_bit=new unsigned char [length]; BeginWaitCursor(); { rij.set_key(k_bit,256); for(int j=0;j<N;j++) { rij.decrypt(var,m_bit); m_bit+=16; var+=16; } m_bit=m_bit-16*N; var=var-16*N; } EndWaitCursor(); char szFilter[] = "all Files(*.*)|*.*||"; CFileDialog FileDlg( FALSE, NULL, NULL, OFN_HIDEREADONLY, szFilter ); FileDlg.m_ofn.lpstrTitle="解密文件存为"; if( FileDlg.DoModal() == IDOK ){ BeginWaitCursor(); CString PathName = FileDlg.GetPathName(); PathName=PathName+name; CFile f; f.Open(PathName, CFile::modeCreate|CFile::modeWrite); f.Write(m_bit,length); f.Close(); CString string; string="notepad"+PathName; WinExec(string,SW_SHOW); m_jiami="解密结束!"; MessageBox(m_jiami); EndWaitCursor(); } delete m_bit; UpdateData(false); Invalidate(true); } void CRijndView::OnEncryptionSelect() { // TODO: Add your control notification handler code here char szFilter[] = "all Files(*.*)|*.*||"; CFileDialog FileDlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter ); if( FileDlg.DoModal() == IDOK ) { BeginWaitCursor(); CString PathName = FileDlg.GetPathName(); CString filename=FileDlg.GetFileName(); CString first=FileDlg.GetFileTitle(); name=filename.Right(filename.GetLength()-first.GetLength()); { CFile f2; if(f2.Open(PathName,CFile::modeRead|CFile::typeBinary )==FALSE) return; length=f2.GetLength(); N=length/16; if(length%16==0) { N=N; } else { length=length+16-length%16; N=N+1; } var=new unsigned char [length]; f2.Read(var,f2.GetLength()); if(f2.GetLength()%16!=0) { for(i=0;i<(int)(16-f2.GetLength()%16);i++) { var[length-16+f2.GetLength()%16+i]=' '; } } f2.Close(); Invalidate(true); } EndWaitCursor(); } } void CRijndView::OnUncryptionSelect() { // TODO: Add your control notification handler code here char szFilter[] = "all Files(*.*)|*.*||"; CFileDialog FileDlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter ); if( FileDlg.DoModal() == IDOK ) { BeginWaitCursor(); CString PathName = FileDlg.GetPathName(); CString filename=FileDlg.GetFileName(); CString first=FileDlg.GetFileTitle(); name=filename.Right(filename.GetLength()-first.GetLength()); { CFile f2; if(f2.Open(PathName,CFile::modeRead|CFile::typeBinary )==FALSE) return; length=f2.GetLength(); var=new unsigned char [length]; N=length/16; f2.Read(var,f2.GetLength()); f2.Close(); Invalidate(true); } EndWaitCursor(); } }